'use strict';

const Service = require('egg').Service;

class DataService extends Service {
 
  async getdata(page,pagesize) {
    //将数据传回去获取一个分页的data
      let {app}=this
    let startindex=(page-1)*pagesize
// return await app.mysql.query('select * from data limit ? , ?',[startindex,pagesize])
  let list= await app.mysql.select('data', {
    limit: pagesize*1, // 返回数据量  一页的数据条数
    offset: startindex*1, // 数据偏移量 开始下标
  })
    let total=await app.mysql.query('select count(*) from data')
    return {
      list,
      total: total[0]['count(*)']
    }
  }
  async adddata(title,nr,loginid)
  {
    let {app}=this
    return await app.mysql.query('insert into  data (title , nr , loginid , time) values (?,?,?,now())',[title,nr,loginid])
  }
  async updata(id,title,nr)
  {
    let {app}=this
    return await app.mysql.query('update data set nr = ? , title = ? , time = now() where id = ? ',[nr,title,id*1])
  }
  async del(id)
  {
   // return await this.app.mysql.query('delete from data where id in (?)', [id])
  // return await this.app.mysql.query('delete from data where id = ?', [id])
   return await this.app.mysql.query('delete from data where id in (?)', [id])
  }
   async xq(id)
  {
   // return await this.app.mysql.query('delete from data where id in (?)', [id])
  // return await this.app.mysql.query('delete from data where id = ?', [id])
   return await this.app.mysql.query('select * from data where id = ?', [id])
  }
}

module.exports = DataService;